import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

import '../ext/color_ext.dart';
import 'basic_icons.dart';

//占位控件
class PlaceholderWidget extends StatelessWidget {
  static PlaceholderWidget xLarge = PlaceholderWidget(
    placeholder: AppPlaceholder.xLarge,
  );
  static PlaceholderWidget large = PlaceholderWidget(
    placeholder: AppPlaceholder.large,
  );
  static PlaceholderWidget normal = PlaceholderWidget(
    placeholder: AppPlaceholder.normal,
  );
  static PlaceholderWidget small = PlaceholderWidget(
    placeholder: AppPlaceholder.small,
  );
  static PlaceholderWidget xSmall = PlaceholderWidget(
    placeholder: AppPlaceholder.xSmall,
  );

  final AppPlaceholder placeholder;
  final IconData? icon;

  const PlaceholderWidget({super.key, required this.placeholder, this.icon});

  @override
  Widget build(BuildContext context) {
    return Container(
      alignment: Alignment.center,
      color: placeholder.background,
      child: Icon(
        icon ?? BasicIcons.placeholder,
        size: placeholder.size,
        color: placeholder.color,
      ),
    );
  }
}

class AppPlaceholder {
  final Color? background;
  final double size;
  final Color? color;

  AppPlaceholder({this.background, this.size = 32, this.color});

  static Map<String, AppPlaceholder> get styles => {
    "xLarge": xLarge,
    "large": large,
    "normal": normal,
    "small": small,
    "xSmall": xSmall,
  };

  static AppPlaceholder from(String key) {
    return styles[key] ?? AppPlaceholder.normal;
  }

  ///超大占位图
  ///大小：128
  ///场景:
  static AppPlaceholder xLarge = AppPlaceholder.fromJson(
    defaultConfig['xLarge'],
  );

  ///大占位图
  ///大小：64
  ///场景：
  static AppPlaceholder large = AppPlaceholder.fromJson(defaultConfig['large']);

  ///普通占位图
  ///大小：32
  ///场景：
  static AppPlaceholder normal = AppPlaceholder.fromJson(
    defaultConfig['normal'],
  );

  ///小占位图
  ///大小：16
  ///场景：
  static AppPlaceholder small = AppPlaceholder.fromJson(defaultConfig['small']);

  ///超小占位图
  ///大小：8
  ///场景：
  static AppPlaceholder xSmall = AppPlaceholder.fromJson(
    defaultConfig['xSmall'],
  );

  static fromJson(Map<dynamic, dynamic>? json) {
    if (json == null) return null;
    return AppPlaceholder(
      background: HexColor(json['background']),
      size: double.parse((json['size'] ?? 32).toString()),
      color: HexColor(json['color']),
    );
  }

  ///默认配置
  static const Map defaultConfig = {
    'xLarge': {'background': '#FFECECEC', 'size': 128, 'color': '#FFD2D2D2'},
    'large': {'background': '#FFECECEC', 'size': 64, 'color': '#FFD2D2D2'},
    'normal': {'background': '#FFECECEC', 'size': 32, 'color': '#FFD2D2D2'},
    'small': {'background': '#FFECECEC', 'size': 16, 'color': '#FFD2D2D2'},
    'xSmall': {'background': '#FFECECEC', 'size': 8, 'color': '#FFD2D2D2'},
  };

  static init(Map? json) {
    xLarge = AppPlaceholder.fromJson(
      json?["xLarge"] ?? defaultConfig['xLarge'],
    );
    large = AppPlaceholder.fromJson(json?["large"] ?? defaultConfig['large']);
    normal = AppPlaceholder.fromJson(
      json?["normal"] ?? defaultConfig['normal'],
    );
    small = AppPlaceholder.fromJson(json?["small"] ?? defaultConfig['small']);
    xSmall = AppPlaceholder.fromJson(
      json?["xSmall"] ?? defaultConfig['xSmall'],
    );
  }
}
